import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    ViewEncapsulation,
} from "@angular/core";
import {
    PlacesServiceProxy,
    PlaceListDto,
    CreateParkingDto,
    UpdateParkingDto,
} from "@shared/service-proxies/service-proxies";
import * as moment from "moment";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import { Paginator } from "primeng/components/paginator/paginator";
import { Table } from "primeng/components/table/table";

import { finalize } from "rxjs/operators";

import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { ActivatedRoute, Router } from "@angular/router";
import { CreateOrEditPlaceModalComponent } from "./create-or-edit-place-modal.component";
import { DebugRenderer2 } from "@angular/core/src/view/services";

@Component({
    selector: "app-park-guard",
    templateUrl: "./places.component.html",
    styleUrls: ["./places.component.css"],
    animations: [appModuleAnimation()],
})
export class PlacesComponent extends AppComponentBase implements OnInit {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("createOrEditPlaceModal")
    createOrEditPlaceModal: CreateOrEditPlaceModalComponent;
    constructor(
        injector: Injector,
        private route: Router,
        private activeRoute: ActivatedRoute,
        private _placesServiceProxy: PlacesServiceProxy
    ) {
        super(injector);
    }
    parkingid: number;
    parkingName: string;
    parkingCoce: string;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    phone: string;
    ngOnInit() {
        this.activeRoute.queryParams.subscribe((params) => {
            this.parkingid = params["parkingid"];
            this.parkingName = params["parkingName"];
            this.parkingCoce = params["parkingCoce"];
            this.neighbourhood_Name = params["neighbourhood_Name"];
            this.neighbourhood_id = params["neighbourhood_id"];
        });

        this.retuen();
    }

    getPlaces(event?: LazyLoadEvent): void {
        this._placesServiceProxy
            .getAll(
                this.parkingid,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getSkipCount(this.paginator, event),
                this.primengTableHelper.getMaxResultCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                console.log(result);
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.totalRecordsCount = result.items.length;
                this.primengTableHelper.hideLoadingIndicator();
                console.log(result.items);
            });
    }
    deleteParkingGuard(place: PlaceListDto): void {
        this.message.confirm(
            this.l("删除车位" + place.placeName + "?"),
            this.l("AreYouSure"),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._placesServiceProxy.delete(place.id).subscribe(() => {
                        this.getPlaces();
                        abp.notify.success(this.l("SuccessfullyDeleted"));
                    });
                }
            }
        );
    }

    retuen(): void {
        if (
            this.parkingid == undefined &&
            this.parkingName == undefined &&
            this.parkingCoce == undefined
        ) {
            this.route.navigateByUrl("app/vehicl-management/parkings");
            this.notify.error("页面访问错误");
        }
    }

    getUserParking() {
        
        if(this.phone == undefined || this.phone == null || this.phone == ""){
            this.notify.warn("请输入查询手机号");
            return;
        }
        if(!(/^1[3456789]\d{9}$/.test(this.phone))){ 
            this.notify.warn("手机号码有误，请重填");  
            return false; 
        } 

        this._placesServiceProxy.getPlaceInfoByPhone(this.phone)
        .subscribe(result=>{
            
            if(result == ""){
                this.route.navigateByUrl(
                    "app/vehicl-management/userparkings?phone=" +
                        this.phone +
                        "&parkingid=" +
                        this.parkingid
                );
            }else{
                this.notify.warn(result); 
            }

        });

        
    }
}
